##### generate appendix result table

library(BBmisc)
library(xtable)
library(stringr)

source("results_utils.R")

res = load2("~/work/coseal/aslib-r/server_scripts/llama_results.RData")

res = dropNamed(res, c("id", "repl", "llama.fun"))
res = sortByCol(res, c("prob", "algo", "model"))
colnames(res) = c("Scenario", "Type", "Model", "Solved", "PAR10", "MCP")

models = selected.models

sink(file = "results.tex")
catf("\\begin{table}[ht]")
scenarios = unique(res$Scenario)
for (i in seq_along(scenarios)) {
  scenario = scenarios[i] 
  r = subset(res, Scenario == scenario)
  r = subset(r, Model %in% models)
  # sort vbs into first row
  r[c(1, 2), ] = r[c(2, 1), ]
  if (i %% 2 != 1) {
    r = dropNamed(r, c("Type", "Model"))
  }
  r$Scenario = NULL

  # set digits for table
  digits = rep(1, ncol(r) + 1)
  digits[length(digits) - 2] = 2
  xt = xtable(r, digits = digits)
  # latex does not like undescore
  scenario2 = str_replace_all(scenario, "_", "-")
  catf("\\subfloat[caption][%s]{", scenario2)
  catf("\\footnotesize{")
  print(xt, "latex", include.rownames = FALSE, floating = FALSE, booktabs = TRUE)
  catf("}")
  catf("}")
  if (i %% 2 == 0L) {
    catf("\\end{table}")
    catf("\\begin{table}[ht]")
  }
}
catf("\\end{table}")
sink(NULL)

